package com.behelpful.demo.gateway.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ReactiveHttpOutputMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.Optional;

/**
 * 负责路由配置，代替设置路由程序类（RouterFunction）配置
 *
 * @author Alay
 * @date 2021-10-28 13:01
 */
//@RestController
@RequestMapping(value = "/swagger-resources")
public class SwaggerResourceAction {

    @Autowired
    private SwaggerResourcesProvider swaggerResourcesProvider;

    @Autowired(required = false)
    private UiConfiguration uiConfiguration;

    @Autowired(required = false)
    private SecurityConfiguration securityConfiguration;


    @GetMapping
    public Mono resourceHandler() {
        return Mono.create(monoSink -> monoSink.success(swaggerResourcesProvider.get()));
    }


    @GetMapping(value = "/configuration/ui")
    public Mono uiHandle() {
        BodyInserter<UiConfiguration, ReactiveHttpOutputMessage> bodyInserter = BodyInserters.fromValue(
                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build())
        );
        Mono<Object> objectMono = Mono.create(monoSink -> monoSink.success(bodyInserter));
        return objectMono;
    }


    @GetMapping(value = "/configuration/security")
    public Mono securityHandle() {
        BodyInserter<SecurityConfiguration, ReactiveHttpOutputMessage> bodyInserter = BodyInserters.fromValue(
                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build())
        );
        return Mono.create(monoSink -> monoSink.success(bodyInserter));
    }

}
